<template>
	<view class="content">
		<view>
			<image class="bg-img" src="../../static/index/img3.jpg"></image>
		</view>
		<view class="aubutton">
			<view class="button">
				<button type="primary" lang="zh_CN" @tap="getUserProfile">微信授权登录</button>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				//临时登录凭证 code
				code: '',
				//名称
				nickName: '',
				//头像地址
				avatarUrl: '',
				//openid
				openid: '',
			}
		},

		methods: {
			getUserProfile() {
				wx.getUserProfile({
					lang: 'zh_CN',
					desc: '用于获取你的个人信息',
					success: res => {
						//判断是否获取到信息
						if (res.userInfo) {
							wx.setStorageSync('nickName', res.userInfo.nickName)
							wx.setStorageSync('avatarUrl', res.userInfo.avatarUrl)
							//获取微信登录接口
							wx.login({
								provider: 'weixin',
								success: res => {
									//把res.code赋值给this.code
									this.code = res.code
									//向后端发送code换取openid
									wx.request({
										url: `https://backendth.flymi.top/openid/${this.code}`,
										success: res => {
											//openid写入本地缓存
											wx.setStorageSync('openid', res.data.openid);
											this.openid = res.data.openid;
											//判断是否openid获取成功
											if (!this.openid) {
												uni.showToast({
													title: '你的微信异常',
													icon: 'error',
													duration: 2000
												})
												wx.removeStorageSync('nickName');
												wx.removeStorageSync('avatarUrl');
												wx.removeStorageSync('openid');
											} else {
												//openid获取成功则请求后端判断是否绑定
												wx.request({
													url: `https://backendth.flymi.top/user/${res.data.openid}`,
													method: "GET",
													success: res => {
														//判断该学号是否绑定openid
														//未绑定则跳转到绑定页面
														if (res.data.code == 402) {
															uni.showToast({
																title: '你尚未绑定学号',
																icon: "error",
																duration: 2000
															})
															uni.navigateTo({
																url: '/pages/register/Register'
															})
														//绑定则跳转首页页面
														} else if (res.data.code == 200) {
															uni.setStorageSync('studentnum',res.data.data.userName);
															uni.showToast({
																title: '登录成功',
																icon: "success",
																duration: 2000
															})
															uni.switchTab({
																url: '/pages/index/index',
															})
														}
													}
												})
											}
										}
									})
								}
							})
						} else {
							uni.showToast({
								title: '获取信息失败',
								icon: "error",
								duration: 2000
							})
						}
					},
					fail: err => {
						uni.showToast({
							title: '你已取消登录',
							icon: "error",
							duration: 2000
						})
					}
				})
			},
		}
	}
</script>

<style>
	.content {
		width: 100vw;
		height: 100%;
	}

	.bg-img {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		z-index: -1;
	}

	.aubutton {
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 15px;
		padding-top: 60%;
	}

	.button {
		width: 100%;
	}
</style>
